package com.example.wechat.mapper;

import java.util.Optional;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.example.wechat.model.WechatConversation;

/**
 * 聊天会话Mapper接口
 * 
 * @author ruoyi
 * @date 2025-11-12
 */
public interface WechatConversationMapper {

	/**
	 * 新增聊天会话
	 * 
	 * @param wechatConversation 聊天会话
	 * @return 结果
	 */
	@Insert("insert into t_wechat_conversation(user_id, target_id, session_type, last_message_id, last_message_content, last_message_time, unread_count, is_pinned, is_muted) " +
			"values (#{userId}, #{targetId}, #{sessionType}, #{lastMessageId}, #{lastMessageContent}, #{lastMessageTime}, #{unreadCount}, #{isPinned}, #{isMuted})")
	@Options(useGeneratedKeys = true, keyProperty = "id")
	public int insertWechatConversation(WechatConversation wechatConversation);

	@Select("select id,user_id,target_id,session_type,last_message_id,last_message_content,last_message_time,unread_count,is_pinned,is_muted,create_time,update_time "
			+ "from t_wechat_conversation where del_flag = 0 and user_id = ${userId} and target_id = ${targetId} and session_type = ${sessionType}")
	public WechatConversation queryByUserIdAndTargetIdAndSessionType(long userId, long targetId, int sessionType);

	@Select("select id,user_id,target_id,session_type,last_message_id,last_message_content,last_message_time,unread_count,is_pinned,is_muted,create_time,update_time,del_flag "
			+ "from t_wechat_conversation where id = ${id}")
	public Optional<WechatConversation> findById(Long id);

	@Update("update t_wechat_conversation set last_message_id = #{lastMessageId}, last_message_content = #{lastMessageContent}, "
			+ "last_message_time = #{lastMessageTime}, unread_count = #{unreadCount}, is_pinned = #{isPinned}, "
			+ "is_muted = #{isMuted}, update_time = #{updateTime} where id = #{id}")
	public int save(WechatConversation conversation);
}
